package LeetCode;

public class LC_309_BestTimetoBuyandSellStockwithCooldown {

    public static void main(String[] args) {

    }

    static class Solution {
        public int maxProfit(int[] prices) {
            if (prices == null || prices.length == 0) return 0;
            int len = prices.length;
            int[] buy = new int[len + 1];
            int[] sell = new int[len + 1];
            int[] cooldown = new int[len + 1];
            buy[0] = Integer.MIN_VALUE;
            for (int i = 1; i < len + 1; i++) {
                buy[i] = Math.max(buy[i - 1], cooldown[i - 1] - prices[i - 1]);
                sell[i] = Math.max(buy[i - 1] + prices[i - 1], sell[i - 1]);
                cooldown[i] = Math.max(sell[i - 1], Math.max(buy[i - 1], cooldown[i - 1]));
            }
            return Math.max(buy[len], Math.max(sell[len], cooldown[len]));
        }

    }
}